package com.fire.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fire.exception.ServiceException;
import com.fire.model.entity.Admin;
import com.fire.mapper.AdminMapper;
import com.fire.response.ResponseCodeEnum;
import com.fire.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-06
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public String login(Admin admin) throws ServiceException {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", admin.getUsername());
        Admin selected = baseMapper.selectOne(wrapper);
        if(null == selected) {
            throw new ServiceException(ResponseCodeEnum.ADMIN_NOT_FOUND);
        }
        if(!selected.getPassword().equalsIgnoreCase(admin.getPassword())){
            throw new ServiceException(ResponseCodeEnum.PASSWORD_ERROR);
        }
        StpUtil.login(selected.getId());
        return StpUtil.getTokenValueByLoginId(selected.getId());
    }
}
